﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using log4net;

namespace AirlineServer
{
    [ServiceContract]
    interface ISearchService
    {
        [OperationContract]
        [QueryCacheOperationBehaviorAttribute]
        List<AirTravel> GetTravelOptions(string src, string dst, DateTime date, List<string> servers);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    internal class SearchService : ISearchService
    {
        private readonly ILog log = LogManager.GetLogger(typeof(SearchService));

        public ClusterManager ClusterManager { get; set; }

        public List<AirTravel> GetTravelOptions(string src, string dst, DateTime date, List<string> servers)
        {
            try
            {
                if (ClusterManager == null)
                {
                    throw new FaultException("Cluster manager was not set");
                }

                return ClusterManager.GetTravelOptions(src, dst, date, servers);
            }
            catch (Exception e)
            {
                if (log.IsWarnEnabled)
                {
                    log.Warn(string.Format("Failed to retrive travel options from {0} to {1} on {2}", src, dst, date), e);
                }

                throw new FaultException("Internal error");
            }
        }
    }
}
